﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace YGWebMobileApi.APITest.Config
{
    public static class MapApiInitXmlReader
    {
        private static Dictionary<string, APINode> DictAPIs;

        static MapApiInitXmlReader()
        {
            DictAPIs = new Dictionary<string, APINode>();
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("Config/MapApiInit.xml"));

            XmlNodeList nodes = doc.SelectNodes("//API");
            foreach (XmlNode node in nodes)
            {
                APINode apiNode = new APINode();
                apiNode.Code = node.Attributes["Code"].Value;
                apiNode.Name = node.Attributes["Name"].Value;

                List<string> list = new List<string>();
                XmlNodeList sns = node.SelectNodes(".//Field");
                foreach (XmlNode s in sns)
                {
                    list.Add(s.InnerText);
                }
                apiNode.Fields = list.ToArray();
                DictAPIs.Add(apiNode.Code, apiNode);
            }
        }

        public static string[] GetAPINodeFields(string code)
        {
            if (DictAPIs.ContainsKey(code))
            {
                return DictAPIs[code].Fields;
            }
            else
            {
                return null;
            }
        }

        public class APINode
        {
            public string Code;
            public string Name;
            public string[] Fields;
        }
    }
}